import { ObjInfo } from '@amcax/base';
import { RJSFSchema } from '@rjsf/utils';
import { Dispatch, SetStateAction } from 'react';
import {
  NumberInputFormat,
  PickInputRJSFFormat,
} from '../../../../common/components/command-form/constants';
import { FormData } from './types';

export const schema: RJSFSchema = {
  properties: {
    base: {
      type: 'string',
      format: PickInputRJSFFormat.PickProduct,
    },
    direction: {
      type: 'string',
      format: PickInputRJSFFormat.PickDirection,
    },
    distance: {
      type: 'number',
      title: '间距1',
      default: 10,
      format: NumberInputFormat.Length,
    },
    count: {
      type: 'number',
      title: '实例数1',
      default: 2,
      minimum: 2,
    },
    useDir2: {
      type: 'boolean',
      title: '使用第二个方向',
      default: false,
    },
    direction2: {
      type: 'string',
      format: PickInputRJSFFormat.PickDirection,
    },
    distance2: {
      type: 'number',
      title: '间距2',
      default: 10,
      format: NumberInputFormat.Length,
    },
    count2: {
      type: 'number',
      title: '实例数2',
      default: 2,
      minimum: 2,
    },
    skipInstances: {
      type: 'boolean',
      title: '跳过实例',
      default: false,
    },
  },
};

export const extraUiSchema = (
  setFormData: Dispatch<SetStateAction<FormData>>,
  props: ObjInfo,
) => ({
  mode: {
    'ui:widget': 'radio',
    'ui:classNames': 'command-form__mode-switch',
  },
  base: {
    'ui:placeholder': '请输入要阵列的部件',
    'ui:autofocus': true,
    'ui:disabled': !!props,
  },
  direction: {
    'ui:placeholder': '为阵列1方向做参考',
  },
  distance: {
    'ui:options': {
      showReverse: true,
      disableParameter: true,
      onReverseChange: (isReverse) => {
        setFormData((prevFormData) => ({
          ...prevFormData,
          reverse: isReverse,
        }));
      },
    },
  },
  direction2: {
    'ui:placeholder': '为阵列2方向做参考',
  },
  distance2: {
    'ui:options': {
      showReverse: true,
      disableParameter: true,
      onReverseChange: (isReverse) => {
        setFormData((prevFormData) => ({
          ...prevFormData,
          reverse2: isReverse,
        }));
      },
    },
  },
});
